<?php 
require_once("../class/NewsList.php");
require_once("../class/PrayList.php");
require_once("../class/AttachmentList.php");
require_once("../class/Tool.php");

$newlist = new NewsList(5); 
$praylist = new PrayList(5); 
$attachmentList = new AttachmentList(); 

if ($_POST["type"] == "news")
{
	if ($_POST["nid"] == "")
	{
		$iid = $newlist->setNews($_POST["subject"], $_POST["message"]);
		if ($_FILES["file_attachment"]["name"] != "")
		{
			$file_path = "../upload/" . $iid;
			if (!file_exists($file_path))
			  mkdir($file_path, 0700);
			move_uploaded_file($_FILES["file_attachment"]["tmp_name"], $file_path . "/" . $_FILES["file_attachment"]["name"]);
			$attachmentList->setAttachment($iid, $_FILES["file_attachment"]["name"]);
		}
		
		$post_data = array('return_message' => 'Create Success!', 'page' => $_POST["page"]);
	}
	else
	{
		$newlist->updateNews($_POST["nid"], $_POST["subject"], $_POST["message"]);
		if ($_FILES["file_attachment"]["name"] != "" & $_POST["file_action"] == "update")
		{
			$file_path = "../upload/" . $_POST["nid"];
			if (!file_exists($file_path))
			  mkdir($file_path, 0700);
			move_uploaded_file($_FILES["file_attachment"]["tmp_name"], $file_path . "/" . $_FILES["file_attachment"]["name"]);
			if ($attachmentList->existAttachment($_POST["nid"]))
			  $attachmentList->updateAttachment($_POST["nid"], $_FILES["file_attachment"]["name"]);
			else
			  $attachmentList->setAttachment($_POST["nid"], $_FILES["file_attachment"]["name"]);
		}
		elseif ($_POST["file_action"] == "delete")
		{
			$attachmentList->deleteAttachment($_POST["nid"]);
		}
		$post_data = array('return_message' => 'Update Success!', 'page' => $_POST["page"]);
	}
	Tool::redirect_post("news_admin.php", $post_data);
}
else
{
	if ($_POST["pid"] == "")
	{
		if($_POST["subject"] != "" & $_POST["message"] != "" & $_POST["prayer"] != "")
		{
			$praylist->setPray($_POST["subject"], $_POST["message"], $_POST["prayer"], $_POST["status"]);
			$post_data = array('return_message' => 'Create Success!', 'page' => $_POST["page"]);
		}
		else
		{
			$post_data = array('return_message' => 'Please fill in all fields!', 'page' => $_POST["page"]);
		}
	}
	else
	{
		if($_POST["subject"] != "" & $_POST["message"] != "" & $_POST["prayer"] != "")
		{
			$praylist->updatePray($_POST["pid"], $_POST["subject"], $_POST["message"], $_POST["prayer"], $_POST["status"]);
			$post_data = array('return_message' => 'Update Success!', 'page' => $_POST["page"]);
		}
		else
		{
			$post_data = array('return_message' => 'Please fill in all fields!', 'page' => $_POST["page"]);
		}
	}
	Tool::redirect_post("pray_admin.php", $post_data);
}

?>
